﻿using System;
using System.Configuration;
using System.Net.Sockets;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Spider.Network
{
    sealed class PandaFormater
    {
        public static PandaConfiguration Deserialize(string config)
        {
            XElement root = XElement.Parse(config);
            PandaConfiguration pandaConfig = new PandaConfiguration();
            pandaConfig.IP = root.Element("IP").Value;
            pandaConfig.Port = int.Parse(root.Element("Port").Value);
            pandaConfig.Backlog = int.Parse(root.Element("Backlog").Value);
            pandaConfig.Protocol = (ProtocolType)Enum.Parse(typeof(ProtocolType), root.Element("Protocol").Value,true);
            pandaConfig.BufferSize = int.Parse(root.Element("BufferSize").Value);
            pandaConfig.Encoding = Encoding.GetEncoding(root.Element("Encoding").Value);
            return pandaConfig;
        }
        public static string Serialize(PandaConfiguration pandaConfig)
        {
            XDocument doc=new XDocument(new XDeclaration("1.0", "utf-8", "yes"),new XElement("Panda", new XElement("IP", XmlEncode(pandaConfig.IP)), new XElement("Port", pandaConfig.Port), new XElement("Backlog", pandaConfig.Backlog), new XElement("Protocol", XmlEncode(pandaConfig.Protocol.ToString())), new XElement("BufferSize", pandaConfig.BufferSize), new XElement("Encoding", XmlEncode(pandaConfig.Encoding.WebName))));
            return doc.ToString();
        }
        private static string XmlEncode(string content)
        {
            if (content==null)
            {
                throw new ArgumentNullException();
            }
            string temp = content;
            temp = temp.Replace("&","&amp;");
            temp = temp.Replace( "<","&lt;");
            temp = temp.Replace(">","&gt;");
            temp = temp.Replace("\'","&apos;");
            temp = temp.Replace("\"","&quot;");
            return temp;
        }
    }
    sealed class PandaConfiguration
    {
        private string ip = "127.0.0.1";
        public string IP
        {
            get { return ip; }
            set { ip = value; }
        }
        private int port = 9999;
        public int Port
        {
            get { return port; }
            set { port = value; }
        }
        private int backlog = 100;
        public int Backlog
        {
            get { return backlog; }
            set { backlog = value; }
        }
        private ProtocolType protocol = ProtocolType.IP;
        public ProtocolType Protocol
        {
            get { return protocol; }
            set { protocol = value; }
        }
        private int bufferSize = 102400;
        public int BufferSize
        {
            get { return bufferSize; }
            set { bufferSize = value; }
        }
        private Encoding encoding = Encoding.UTF8;
        public Encoding Encoding
        {
            get { return encoding; }
            set { encoding = value; }
        }
    }
}
